﻿using DH.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DH.MQ
{
    public abstract class BaseMessage
    {
        //-----------------------Edit by Ron.Shen-------------------------------//
        /// <summary>
        /// 消息ID，建议采用生产者的GUID
        /// </summary>
        public string MessageID { get; set; }

        /// <summary>
        /// 是否需要确认
        /// </summary>
        private bool isNeedAck=false;

        public bool IsNeedAck
        {
            get { return isNeedAck; }
            set { isNeedAck = value; }
        }
       
        //----------------------------------------------------------------------//


        public virtual  string BuildMessage(BaseMessage bm,out string MessageID)
        {
            MessageID = System.Guid.NewGuid().ToString();
            bm.MessageID = MessageID;
            var result =Newtonsoft.Json.JsonConvert.SerializeObject(bm);
            return result;
        }

        public abstract BaseMessage GetMessageResult(string bm);
        //{
        //    BaseMessage result = Newtonsoft.Json.JsonConvert.DeserializeObject<BaseMessage>(bm);

        //    return result;
        //}
    }
    //-----------------------Edit by Ron.Shen-------------------------------//
    public class AckMessage
    {
        public string MessageID { get; set; }
        public AckOperation Operation { get; set; }


        private int ackTime;
        
        public void SetTimeStamp()
        {
            DateTime dt = System.DateTime.Now;
            try
            {
                ackTime = DateTimeToStamp(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        private int DateTimeToStamp(System.DateTime time)
        {

            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
        //public int AckTime
        //{
        //    get { return AckTime; }
        //     set { AckTime = value; }

        //}//确认时间，为防止发送两条确认消息后，按照最新一条确认时间进行确认还是废弃的处理

        public int AckTime
        {
            get
            {
                return ackTime;
            }

            private  set
            {
                ackTime = value;
            }
        }
    }
    public enum AckOperation
    {
        确认=0,
        废弃=1
    }
}
